<?php
/**
 * This is the index.ctp of Administration Controller
 * @author Amissah Gideon<kuulmek@yahoo.com>
 * @access public
 * @version 1.0
 */

$OrgObj = ClassRegistry::init('Organisation');
# Get the Organisation data
$Org = $OrgObj->find('first', array('recursive' => -1));

# Instance of Module Model
$moduleObj =  ClassRegistry::init('Module');
# List of all the modules in the system
$allModules = $moduleObj->find('all', array('conditions'=>array('Module.active'=>1),'recursive' => -1, 'order' => 'Module.name ASC')); 

/**
# Instance of Module Model For Config
$moduleConfigObj =  ClassRegistry::init('Module');
# List of all the modules in the system
//$allConfigModules = $moduleConfigObj->find('all', array('recursive' => -1, 'order' => 'Module.name ASC'));
$allConfigModules = Set::combine($moduleConfigObj->find('all', array('recursive' => -1, 'order' => 'Module.name ASC')), '{n}.Module.id', '{n}.Module');
*/

$GroupObj =  ClassRegistry::init('Group');
$group_data = Set::combine($GroupObj->find('all',array('fields'=>array('id','name'),'conditions' => array('deleted'=>'n','active'=>'y'),'recursive'=>-1)),'{n}.Group.id', '{n}.Group');
$groupOpt = array();
foreach($group_data as $value){
    $groupOpt[$value['id']] = $value['name'];
}

$configObj =  ClassRegistry::init('Configuration');
# List of all the modules in the system
$allConfiguration =  Set::combine($configObj->find('all', array('recursive' => -1, 'order' => 'Configuration.key ASC')), '{n}.Configuration.id', '{n}.Configuration');

$loggedUser =  $authUser;
$loggedUser_id = $loggedUser['id'];
$modified_date = date('Y-m-d H:i:s');
$created_date = date('Y-m-d H:i:s');

?>

<div class="user-pane" >
   <div class="pane-header">
          <?php  echo __('System Users Panel') ; ?>
   </div>
   
   <table id="userflex" style="display:none;"></table>
   
   <div style="display: none;">
      <div id="user-form-window" style="margin: 20px 5px 5px 5px;">
    	<div class="user-prompt" ></div>
    	<!--<form id="user-form" class="user-form" name="user-form" > -->
         <?php echo $this->Form->create('User', array('default' => false, 'id' => 'user-form', 'action' => 'add','inputDefaults' => array('label' => false,'div' => false)));?>	
           <table cellspacing="10">
                <tr>
                    <td><?php echo __('User Name:') ;?> </td>
                    <td> 
                        <?php echo $this->Form->input('username', array('div' => false, 'label' => false, 'id'=>'username' ,'class' => 'validate[required,custom[noSpecialCaracters]] username', 'value'=>'')); ?>
                        <!--<input name="username" type="text" class="validate[required,custom[noSpecialCaracters]] username"  id="username" value="" /> -->
                    </td>
                </tr>
                <tr>
                    <td><?php  echo __('First Name:') ;?> </td>
                    <td> 
                        <?php echo $this->Form->input('first_name', array('div' => false, 'label' => false, 'id'=>'first_name' ,'class' => 'validate[required,custom[noSpecialCaracters]] first_name', 'value'=>'')); ?>
                      <!--  <input name="first_name" type="text" class="validate[required,custom[noSpecialCaracters]] first_name"  id="first_name" value="" /> -->
                    </td>
                </tr>
                <tr>
                    <td><?php  echo __('Last Name:') ;?> </td>
                    <td>
                        <?php echo $this->Form->input('last_name', array('div' => false, 'label' => false, 'id'=>'last_name' ,'class' => 'validate[required,custom[noSpecialCaracters]] last_name', 'value'=>'')); ?> 
                      <!--  <input name="last_name" type="text" class="validate[required,custom[noSpecialCaracters]] last_name"  id="last_name" value="" /> -->
                    </td>
                </tr>
               	<tr>
                    <td><?php  echo __('Password') ;?></td>
				    <td>
                    <?php echo $this->Form->input('password', array('type'=>'password', 'div' => false, 'label' => false, 'id'=>'password' ,'class' => 'validate[required,custom[noSpecialCaracters]] password', 'value'=>'')); ?>
                      <!--  <input type="password" name="password"  id="password" class="validate[required] password"/> -->
                    </td>
                </tr>
                <tr>
                    <td><?php  echo __('Group:') ;?></td>
				    <td>
                        <?php echo $this->Form->input("group_id", array('id'=>'group_id', 'options' => $groupOpt));?> 
                    </td>
                </tr>
                <tr>
                    <td><?php  echo __('Active:') ;?></td>
				    <td>
                        <?php echo $this->Form->input("active", array('id'=>'active', 'options' => array('y'=>'Active', 'n'=>'Inactive')));?> 
                    </td>
                </tr>
                <tr>
                   <td>
                       
                   </td>
                   <td> 
                       <button type="button" class="save" id="user-save-btn"><?php echo $this->Html->image('ico-49.gif', array('width' => '16', 'height' => '16', 'align' => 'left', 'border' => '0')); ?>&nbsp;Save</button>
                       <button type="reset" class="cancel" id="user-cancel-btn"><?php echo $this->Html->image('ico-43.png', array('width' => '16', 'height' => '16', 'align' => 'left', 'border' => '0')); ?>&nbsp;Cancel</button>
                   </td>
                </tr>
            </table>
          <?php echo $this->Form->end();?>
    <!--	</form> -->
        <input type="hidden" name="modules_count" id="modules_count_id" value="<?php echo count($allModules);?>"/> 	
      </div>
    </div>
      
</div>



<div class="group-pane" style="display: none;">
   <div class="pane-header">
          <?php  echo __('Group Panel') ;?>
   </div>
   
   <table id="groupflex" style="display:none;"></table>
   
   <div style="display: none;">
      <div id="group-form-window" style="margin: 20px 5px 5px 5px;">
    	<div class="group-prompt" ></div>
         <?php echo $this->Form->create('Group', array('default' => false, 'id' => 'group-form', 'action' => 'add','inputDefaults' => array('label' => false,'div' => false)));?>	
           <table cellspacing="10">
                <tr>
                    <td><?php echo __('Group Name:') ;?> </td>
                    <td> 
                        <?php echo $this->Form->input('name', array('div' => false, 'label' => false, 'id'=>'name' ,'class' => 'validate[required] name', 'value'=>'')); ?>
                    </td>
                </tr>
                <tr>
                    <td><?php  echo __('Active:') ;?></td>
				    <td>
                        <?php echo $this->Form->input("active", array('id'=>'active', 'options' => array('y'=>'Active', 'n'=>'Inactive')));?> 
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><?php  echo __('Select Group Modules') ;?></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div style="height: 150px; overflow: auto; padding: 5px; border:1px solid #b2b2b2; border-radius: 5px;">
                         <?php
                            if(!empty($allModules)){
                                $module_count = 0;
                                foreach($allModules as $module){
                                 ?>
                                    <div><?php echo $this->Form->input("ModulesGroup.$module_count.module_id", array('type'=>'checkbox', 'div' => false, 'label' => false, 'class' => '', 'value'=>''.$module['Module']['id'].'')); ?><?php echo $module['Module']['name']; ?></div>
                                 <?php
                                 $module_count++;
                                }
                            }
                         ?>
                        </div>
                   </td>
                </tr>
                <tr>
                   <td>
                
                   </td>
                   <td> 
                       <button type="button" class="save" id="group-save-btn"><?php echo $this->Html->image('ico-49.gif', array('width' => '16', 'height' => '16', 'align' => 'left', 'border' => '0')); ?>&nbsp;Save</button>
                       <button type="reset" class="cancel" id="group-cancel-btn"><?php echo $this->Html->image('ico-43.png', array('width' => '16', 'height' => '16', 'align' => 'left', 'border' => '0')); ?>&nbsp;Cancel</button>
                   </td>
                </tr>
            </table>
          <?php echo $this->Form->end();?>
      </div>
    </div>
</div>


<div class="config-pane" style="display: none;">
      <!--   <form id="Org-form" name="Org-form"> -->
<?php echo $this->Form->create('Organisation', array('default' => false, 'id' => 'org-form', 'url' => array('controller' => 'Administration', 'action' => 'add'),'inputDefaults' => array('label' => false,'div' => false)));?>
       <div class="pane-header">
              <?php  echo __('Organisation Settings Panel') ;?>
       </div>
       <div id="config-details-box">
              <div class="pane-sub-header">
                     <strong><?php  echo __('Basic Information'); ?></strong>
              </div>
              <div id="details-form-window">
              <table width="100%">
                       <tr>
                         <td align="left" valign="middle" nowrap ><?php echo __('Name'); ?></td>
                         <th align="left" valign="middle">
                               <!-- <input type="text" name="church-name" id="church-name" value="<?php //echo $church['Church']['church_name'];  ?>" class="validate[required] church-name" /> -->
                             <?php echo $this->Form->input('name', array('div' => false, 'label' => false, 'class' => 'validate[required]', 'value'=>''.$Org['Organisation']['name'].'')); ?>
                         </th>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Registration Number'); ?></td>
                         <td align="left" valign="middle">
                               <!--  <input name="registration-number" id="registration-number" value="<?php //echo $church['Church']['registration_number'];  ?>"  class="validate[required]  registration-number" /> -->
                                 <?php echo $this->Form->input('registration_number', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['registration_number'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Vision'); ?></td>
                         <td align="left" valign="middle">
                              <!--   <textarea name="vision" id="vision" cols="25" rows="3">
                                <?php //echo $church['Church']['vision'];  ?></textarea>  -->
                                <?php echo $this->Form->input('vision', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['vision'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Missions');?></td>
                         <td align="left" valign="middle"> 
                            <!--  <textarea name="missions" id="missions"><?php //echo $church['Church']['missions'];  ?></textarea> -->
                             <?php echo $this->Form->input('missions', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['missions'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Date Of Establishment');?></td>
                         <td align="left" valign="middle">
                          <!-- <input type="text" name="birthday" id="birthday" class="date-picker" value="<?php //echo $church['Church']['church_birthday'];  ?>" />  -->
                               <?php echo $this->Form->input('date_of_establishment', array('type'=>'text','div' => false, 'label' => false, 'class' => 'date-picker', 'size' => '50', 'value'=>''.$Org['Organisation']['date_of_establishment'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('History');?></td>
                         <td align="left" valign="middle">
                             <!-- <textarea name="history" id="history" cols="25" rows="3"><?php //echo $church['Church']['church_history'];  ?></textarea>  -->
                              <?php echo $this->Form->input('history', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['history'].'')); ?> 
                         </td>
                       </tr>
                     </table>
              </div>
       </div>
       <div id="config-address-box">
              <div class="pane-sub-header">
                     <strong><?php  echo __('Address Information'); ?></strong>
              </div>
              <div id="address-form-window">
              <table width="100%">
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Location Name');?></td>
                         <th align="left" valign="middle"> 
                            <!-- <input type="text" name="location" id="location" value="<?php //echo $church['Church']['location'];  ?>" /> -->
                             <?php echo $this->Form->input('location', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['location'].'')); ?>
                         </th>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('City');?></td>
                         <td align="left" valign="middle"> 
                            <!-- <input type="text" name="city" id="city" value="<?php //echo $church['Church']['city'];  ?>"> -->
                             <?php echo $this->Form->input('city', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['city'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Country');?></td>
                         <td align="left" valign="middle"> 
                            <!--  <input type="text" name="country" id="country" value="<?php //echo $church['Church']['country'];  ?>"> -->
                             <?php echo $this->Form->input('country', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['country'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Street Number');?></td>
                         <td align="left" valign="middle"> 
                            <!-- <input type="text" name="street-number" id="street-number" value="<?php //echo $church['Church']['street_number'];  ?>"> -->
                             <?php echo $this->Form->input('street_number', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['street_number'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('ZipCode');?></td>
                         <td align="left" valign="middle"> 
                            <!-- <input type="text" name="zipcode" id="zipcode" value="<?php //echo $church['Church']['zipcode'];  ?>"> -->
                             <?php echo $this->Form->input('zipcode', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['zipcode'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Telephone 1');?></td>
                         <td align="left" valign="middle"> 
                            <!-- <input type="text" name="telephone-1" id="telephone-1" value="<?php //echo $church['Church']['telephone_1'];  ?>"> -->
                             <?php echo $this->Form->input('telephone_1', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['telephone_1'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Telephone 2');?></td>
                         <td align="left" valign="middle">
                             <!-- <input type="text" name="telephone-2" id="telephone-2" value="<?php //echo $church['Church']['telephone_2'];  ?>"> -->
                             <?php echo $this->Form->input('telephone_2', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['telephone_2'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Fax Number')?></td>
                         <td align="left" valign="middle"> 
                            <!-- <input type="text" name="fax-number" id="fax-number" value="<?php //echo $church['Church']['fax_number'];  ?>"> -->
                             <?php echo $this->Form->input('fax_number', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['fax_number'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Email 1');?></td>
                         <td align="left" valign="middle"> 
                            <!-- <input type="text" name="email-1" id="email-1" value="<?php //echo $church['Church']['email_1'];  ?>"> -->
                             <?php echo $this->Form->input('email_1', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['email_1'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Email 2');?></td>
                         <td align="left" valign="middle"> 
                            <!-- <input type="text" name="email-2" id="email-2" value="<?php //echo $church['Church']['email_2'];  ?>"> -->
                             <?php echo $this->Form->input('email_2', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['email_2'].'')); ?>
                         </td>
                       </tr>
                       <tr>
                         <td align="left" valign="middle" nowrap><?php echo __('Postal Box');?></td>
                         <td align="left" valign="middle">
                             <!-- <textarea name="postal-box" id="postal-box" cols="25" rows="3"><?php //echo $church['Church']['postal_box'];  ?></textarea> -->
                             <?php echo $this->Form->input('postal_box', array('div' => false, 'label' => false, 'class' => '', 'value'=>''.$Org['Organisation']['postal_box'].'')); ?>
                        </td>
                       </tr>
                     </table>
              </div>
       </div>
        <div id="config-actions-buttons">
                 <?php echo $this->Form->button($this->Html->image('ico-49.gif', array('width' => '16', 'height' => '16', 'align' => 'left', 'border' => '0')).'&nbsp;'. __('Save',true), array('div' => false, 'label' => false, 'class' => 'save','id' => 'save-btn', 'type'=>'submit')); ?>
               <!-- <button type="submit" name="save-btn"  class="save" id="save-btn"><?php  //echo __('Save Settings') ;?></button> -->
                <?php echo $this->Form->button(__('Upload Logo',true), array('div' => false, 'label' => false, 'class' => 'upload','id' => 'upload-btn', 'type'=>'submit')); ?>
               <!-- <button type="submit" name="upload-btn"  class="upload" id="upload-btn" ><?php  //echo __('Upload Logo') ;?></button> -->
                <?php echo $this->Form->button($this->Html->image('ico-43.png', array('width' => '16', 'height' => '16', 'align' => 'left', 'border' => '0')).'&nbsp;'. __('Cancel',true), array('div' => false, 'label' => false, 'class' => 'cancel','id' => 'cancel-btn', 'type'=>'reset')); ?>
               <!-- <button  type="reset"name="cancel-btn"  class="cancel" id="cancel-btn"><?php // echo __('Cancel') ;?></button>  -->
        </div>
         <?php echo $this->Form->input("Organisation.id", array('type'=>'hidden','id'=>'org-id','label' => false, 'value'=>''.$Org['Organisation']['id'].'')); ?> 
         <!-- <input type="hidden" name="church-id" id="church-id" value="<?php //echo $church['Church']['id'];  ?>" />  -->
 <?php echo $this->Form->end();?> 
</div>


<div class="system-config-pane" style="display: none;" >
       <!-- <form id="system-configuration-form" name="system-configuration-form"> -->
    <?php echo $this->Form->create('Configuration', array('default' => false, 'id' => 'system-configuration-form', 'url' => array('controller' => 'Administration', 'action' => 'add'),'inputDefaults' => array('label' => false,'div' => false)));?>
       <div class="pane-header">
              <?php  echo __('System Configuration') ;?>
       </div>
       <br />
       <div id="config-details-box" style="float:left; width: 60%;">
              <div class="pane-sub-header">
                     <strong><?php  echo __('Edit System Configuration'); ?></strong>
              </div>
              <div id="details-form-window" style='padding:10px;'>
                  <?php
                    if(!empty($allConfiguration)){
                       // echo debug($allConfiguration);
                        
                        foreach($allConfiguration as $config_key => $config_value_array){
                            $config_value = $config_value_array['value'];
                            $config_description = $config_value_array['description'];
                            
                            echo "<div style='margin-bottom:20px; border-bottom:1px solid #ddd; ' >";
                                echo "<div style='margin-bottom:5px;'> $config_description </div>";
                                echo "<div>".$this->Form->input("Configuration.$config_key.value", array('type'=>'text', 'size'=>'50', 'label' => false, 'class' => 'validate[required] ','value'=>"$config_value"))."</div>"; 
                                echo "<div>";
                                    echo $this->Form->input("Configuration.$config_key.id", array('label' => false, 'value'=>"$config_key"));
                                    echo $this->Form->input("Configuration.$config_key.modified_by", array('type'=>'hidden','label' => false, 'value'=>"$loggedUser_id"));
                                    echo $this->Form->input("Configuration.$config_key.modification_date", array('type'=>'hidden','label' => false, 'value'=>"$modified_date"));
                                echo "</div>"; 
                            echo "</div>";
                        }
                    }
                  ?>
              </div>
       </div>
       <div id="config-actions-buttons">
               <?php echo $this->Form->button($this->Html->image('ico-49.gif', array('width' => '16', 'height' => '16', 'align' => 'left', 'border' => '0')).'&nbsp;'. __('Save Settings',true), array('div' => false, 'label' => false, 'class' => 'save','id' => 'system-config-btn', 'type'=>'submit')); ?>
               
        </div>
<?php echo $this->Form->end();?>
  <!--  </form> -->
</div>

<!-- Users URL -->
<input type="hidden" id="table-url" value="<?php echo $this->Html->url(array('controller' => 'Administration', 'action' => 'getUsers')); ?>" />
<input type="hidden" id="add-edit-url" value="<?php echo $this->Html->url(array('controller' => 'Administration', 'action' => 'add')); ?>" />
<input type="hidden" id="load-user-url" value="<?php echo $this->Html->url(array('controller' => 'Administration', 'action' => 'loadUser')); ?>" />
<input type="hidden" id="delete-url" value="<?php echo $this->Html->url(array('controller' => 'Administration', 'action' => 'deleteUsers')); ?>" />
<!-- Group URL -->
<input type="hidden" id="group-table-url" value="<?php echo $this->Html->url(array('controller' => 'Administration', 'action' => 'getGroups')); ?>" />
<input type="hidden" id="group-add-edit-url" value="<?php echo $this->Html->url(array('controller' => 'Administration', 'action' => 'add')); ?>" />
<input type="hidden" id="load-group-url" value="<?php echo $this->Html->url(array('controller' => 'Administration', 'action' => 'loadGroup')); ?>" />
<input type="hidden" id="group-delete-url" value="<?php echo $this->Html->url(array('controller' => 'Administration', 'action' => 'deleteGroups')); ?>" />